package com.bd.framework.utils;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.TimeZone;

public class UtilsTool 
{
	public static final String COMMON_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
    /**
     * 根据给定格式得到当前日期时间
     * @param fmt 需要的日期格式 
     * @return 符合格式要求的日期字符串 返回格式一般应为yyyy-MM-dd HH:mm:ss:SS
     */
    public static String getDate(String fmt) {
        Date myDate = new Date(System.currentTimeMillis());
        SimpleDateFormat sDateformat = new SimpleDateFormat(fmt);
        sDateformat.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        return sDateformat.format(myDate).toString();
    }
    
    /**
     * 比较两个字符串时间和当前时间,
     * 如果strBrfore距离当前近,返回true,否则返回false
     * @param strBrfore 字符串时间 yyyy-MM-dd HH:mm:ss
     * @param strAfter 字符串时间 yyyy-MM-dd HH:mm:ss
     * @return boolean
     */
    public static boolean dateCompare(String strBrfore,String strAfter) {  
    	long date = formatDateString(strBrfore).getTime() - formatDateString(strAfter).getTime();
    	if(date < 0)
    		return true;
    	else
    		return false;
	}
    
    /**
     * 将指定日期格式化
     * @param fmt
     * @param date
     * @return
     */
    public static String getDate(String fmt,Date date) {
        SimpleDateFormat sDateformat = new SimpleDateFormat(fmt);
        return sDateformat.format(date).toString();
    }

    /**
     * 格式化时间
     * @param dateString String
     * @return Date
     */
    public static Date formatDateString(String dateString) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date startDate = null;
        try {
            startDate = sdf.parse(dateString);
        } catch (ParseException ex) {
            startDate = null;
        }
        return startDate;
    }
    /**
     * 把字符串的时间转变成long型
     * @param str 字符串时间
     * @param mode 字符串时间格式
     * @return Date
     */
    public static long parse(String str,String mode) {  
		Date date=null;
		SimpleDateFormat formatter = new SimpleDateFormat(mode);		
		try {
			 date=formatter.parse(str);    
		} catch (ParseException e) {					
			e.printStackTrace();
		}
	   return date.getTime();
	}
    /**
     * 根据给定格式得到指定日期时间
     * @param fmt 需要的日期格式
     * @param datelong 需要转换的long型时间
     * @return 
     */
    public static String getLongbyDate(String fmt,long datelong) {
        Date myDate = new Date(datelong);
        SimpleDateFormat sDateformat = new SimpleDateFormat(fmt);
        return sDateformat.format(myDate).toString();
    }
    /**
	 * 获得本地机器的IP地址
	 * */
	public static String getIpAdress(){
		String ipAdress = "";
		@SuppressWarnings("rawtypes")
		Enumeration allNetInterfaces;
		try {
			allNetInterfaces = NetworkInterface.getNetworkInterfaces();
			InetAddress ip = null;
			while (allNetInterfaces.hasMoreElements())
			{
				NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
				//System.out.println(netInterface.getName());
				@SuppressWarnings("rawtypes")
				Enumeration addresses = netInterface.getInetAddresses();
				while (addresses.hasMoreElements())
				{
					ip = (InetAddress) addresses.nextElement();
					if (ip != null && ip instanceof Inet4Address)
					{	
						if(!"127.0.0.1".equals(ip.getHostAddress()))
							ipAdress = ip.getHostAddress();
						//System.out.println("本机的IP = " + ip.getHostAddress());
					}
				}
			}
		} catch (SocketException e) {
			e.printStackTrace();
		}
		return ipAdress;
	}
	
    public static void main(String[] ss)
    {
    	Long x = formatDateString("2011-09-01 09:00:00").getTime();
    	x = x + (50+1)*100000;
    	String testdate =getLongbyDate("yyyy-MM-dd HH:mm:ss",x);
    	System.out.println("testdate = "+testdate);
    	System.out.println("&&&&&&&&&&&"+UtilsTool.getDate("yyyy-MM-dd HH:mm:ss"));
    	//System.out.println(DateTools.dateCompare("", "2012-09-01 09:00:00"));
    	System.out.println(UtilsTool.getIpAdress());
    }
}
